package com.umeox.watch.moto.chat.ui

import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.RemoteException
import android.text.SpannableString
import android.text.Spanned
import android.text.style.UnderlineSpan
import android.util.Log
import android.widget.LinearLayout
import android.widget.TextView
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.watch.moto.chat.R
import com.umeox.watch.moto.chat.db.ContactInfo
import com.umeox.watch.moto.chat.widget.SunmitDialog
import com.umeox.watch.moto.dataservice.proxy.PushApiClientProxy


class GroupSettingActivity : ThemeAppCompatActivity() {

    companion object {
        fun intoGroupSettingActivity(context: Context, contactInfo: ContactInfo) {
            context.startActivity(Intent(context, GroupSettingActivity::class.java).apply {
                putExtra("contactInfo", contactInfo)
            })
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_group_setting)
        val contactInfo = intent?.getParcelableExtra<ContactInfo>("contactInfo")
        if (contactInfo == null) {
            finish()
            return
        }


        val llAdd = findViewById<LinearLayout>(R.id.llAdd)
        val llDelete = findViewById<LinearLayout>(R.id.llDelete)
        val tvDisband = findViewById<TextView>(R.id.tvDisband)
        llAdd.setOnClickListener {
            AddGroupFriendActivity.intoAddGroupFriendActivity(this, contactInfo.friendId)
        }
        llDelete.setOnClickListener {
            DeleteGroupFriendActivity.intoDeleteGroupFriendActivity(this, contactInfo.friendId)
        }

        val text = tvDisband.text.toString()
        // 创建 Spannable 对象
        val spannableString = SpannableString(text)
        spannableString.setSpan(UnderlineSpan(), 0, text.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        tvDisband.text = spannableString

        tvDisband.setOnClickListener {
            val dialog = SunmitDialog(this)
            dialog.setListener {
                try {
                    val groupId = contactInfo.friendId
                    quitGroup(groupId)
                } catch (e: Exception) {
                    e.printStackTrace()
                } finally {
                    dialog.dismiss()
                    startActivity(Intent(this, ChatContactListActivity::class.java))
                    finish()
                }
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                dialog.create()
            }
            dialog.setTitle(getString(R.string.do_you_want_to_disband_the_group_chat))
            dialog.show()
        }
    }

    @Throws(RemoteException::class)
    fun quitGroup(groupId: String) {
        PushApiClientProxy.fetchBinder().quitGroup(groupId)
    }
}